import { FakeApi, random } from '@wowon/utils';
import { userList } from '../access/user';
import { getCurrentUser } from '../../auth-center';

const initData = Array.from({ length: random(50, 200) }).map(() => {
  const user = userList.pick();
  return {
    user,
    user_id: user.id,
    content: random([
      '用户怎么不能修改密码的？',
      '什么时候系统能稳定点，怎么老是崩！！！😡😡😡什么垃圾系统😤',
      '有没有客服在啊？',
      '工单提交失败的时候能不能不要关掉弹窗啊，我辛苦半天填了表单，提交失败竟然还清空了，做系统的开发，能不能用点心？',
      '歪比巴卜，歪比歪比...',
      `### 有个问题咨询一下
😅请问工单审核什么时候上线呐？
      `,
    ]),
  };
});

export const feedbackList = new FakeApi(initData, {
  queryType: {
    user_id: 'is',
    content: 'like',
  },
});

export default {
  'GET /mock/feedback/query': async (req, res) => {
    const currentUser = getCurrentUser(req);
    if (!currentUser) {
      res.statusCode = 401;
      res.json({ code: '-1', message: '权限不足' });
      return;
    }
    const result = await feedbackList.query(req.query);
    res.json(result);
  },
  'POST /mock/feedback/create': async (req, res) => {
    const currentUser = getCurrentUser(req);
    if (!currentUser) {
      res.statusCode = 401;
      res.json({ code: '-1', message: '权限不足' });
      return;
    }
    const result = await feedbackList.create(req.body);
    res.json(result);
  },
  'POST /mock/feedback/update': async (req, res) => {
    const currentUser = getCurrentUser(req);
    if (!currentUser) {
      res.statusCode = 401;
      res.json({ code: '-1', message: '权限不足' });
      return;
    }
    const result = await feedbackList.update(req.body);
    res.json(result);
  },
  'POST /mock/feedback/remove': async (req, res) => {
    const currentUser = getCurrentUser(req);
    if (!currentUser) {
      res.statusCode = 401;
      res.json({ code: '-1', message: '权限不足' });
      return;
    }
    const result = await feedbackList.remove(req.body);
    res.json(result);
  },
};
